<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : Withdraw.php
 * Created on : 2023-4-6 13:11:43
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\admin\controller;

use app\Request;
use haoteam\service\UtilService;
use app\models\UserWithdraw as UserWithdraw;

/**
 * 提现管理
 * Class Withdraw
 * Tiger <1192851302@qq.com> 2023-4-6 13:11:43
 */
class Withdraw {

    /**
     * 提现列表
     * @auth true
     * @menu true
     * @author Tiger <1192851302@qq.com>
     * @desc    提现列表
     * @param Request $request
     * @return type
     */
    public function index(Request $request) {
        $post = UtilService::postMore([
                    'date',
                    'status',
                    'search'
        ]);
        return app('json')->successful(UserWithdraw::getList($post));
    }

    /**
     * 拒绝提现
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    拒绝提现
     * @param Request $request
     * @return type
     */
    public function refuse(Request $request) {
        [$id] = UtilService::postMore(['id'], $request, true);
        $userWithdraw = UserWithdraw::where('id', $id)->find();
        if (empty($userWithdraw)) {
            return app('json')->fail('参数错误');
        }

        $userModel = new \app\models\User;
        $user = $userModel->where('id', $userWithdraw->user_id)->find();
        $userModel->beginTrans();
        try {
            $user->money = bcadd($user->money, $userWithdraw->money, 2);
            $user->save(); //返还用户
            //写入账单
            \app\models\UserBill::income($user->id, $userWithdraw->id, '提现失败返还余额', 'money', 'withdraw_refuse', $userWithdraw->money, $user->money, 1);
            $userWithdraw->status = 1;
            $userWithdraw->save();
            $userModel->commitTrans();
        } catch (\Exception $exc) {
            $userModel->rollbackTrans();
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('处理成功');
    }

    /**
     * 同意提现
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @desc    同意提现
     * @param Request $request
     * @return type
     */
    public function agree(Request $request) {
        [$id] = UtilService::postMore(['id'], $request, true);
        $userWithdraw = UserWithdraw::where('id', $id)->find();
        if (empty($userWithdraw)) {
            return app('json')->fail('参数错误');
        }
        $userModel = new \app\models\User;
        $user = $userModel->where('id', $userWithdraw->user_id)->field(['openid'])->find();
        $admin_name = \app\models\AdminConfig::getOne('admin_name');

        $userModel->beginTrans();
        try {
            $result = \haoteam\service\EasyWeChatService::payment()->transfer->batches([
                'out_batch_no' => $userWithdraw->out_batch_no, // 商户订单号，需保持唯一性(只能是字母或者数字，不能包含有符号)
                'batch_name' => $admin_name . "余额提现",
                'batch_remark' => $admin_name . '余额提现',
                'total_amount' => ($userWithdraw->money * 100), // 企业付款金额，单位为分
                'total_num' => 1,
                'transfer_detail_list' => [[
                'out_detail_no' => $userWithdraw->out_detail_no,
                'transfer_amount' => ($userWithdraw->money * 100),
                'transfer_remark' => $admin_name . "余额提现",
                'openid' => $user->openid
                    ]],
            ]);
            \think\facade\Log::debug('余额提现 | ' . json_encode($result, JSON_UNESCAPED_UNICODE));
            if (isset($result['batch_id']) == false) {
                throw new \think\Exception('未知错误');
            }
            $userWithdraw->batch_id = $result['batch_id'];
            $userWithdraw->status = 1;
            $userWithdraw->save();
            \app\models\UserBill::update(['status' => 1], ['user_id' => $userWithdraw->user_id, 'link_id' => $userWithdraw->id,
                'pm' => 0, 'category' => 'money', 'type' => 'withdraw']);
            $userModel->commitTrans();
        } catch (\Exception $exc) {
            $userModel->rollbackTrans();
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('处理成功');
    }

}
